package com.auto.nastoolbox.ui

import android.content.res.Configuration.UI_MODE_NIGHT_YES
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.NavigationDrawerItemDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.auto.nastoolbox.NasDestinations
import com.auto.nastoolbox.NasNavigationActions
import com.auto.nastoolbox.R
import com.auto.nastoolbox.theme.NasToolBoxTheme

/**
 * Created by YuanWei1 on 2025/6/3.
 *
 * Description:
 */

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AppDrawer(
    currentRoute: String, navDestination: NasNavigationActions, closeDrawer: () -> Unit, modifier: Modifier = Modifier
) {
    ModalDrawerSheet(
        modifier = modifier,
    ) {
        val context = LocalContext.current // 在@Composable函数内获取
        DrawerTop(modifier = Modifier.padding(horizontal = 28.dp, vertical = 10.dp))
        NavigationDrawerItem(
            label = { Text("首页") },
            icon = { Icon(Icons.Filled.Home, null) },
            selected = currentRoute == NasDestinations.HOME_ROUTE,
            onClick = {
                navDestination.navigateToHome()
                closeDrawer()
            },
            modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding))
        NavigationDrawerItem(
            label = { Text("设置页") },
            icon = { Icon(Icons.Filled.Settings, null) },
            selected = currentRoute == NasDestinations.SETTING,
            onClick = {
                navDestination.navigateToSetting()
                closeDrawer()
            },
            modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding))
        Text("安装的应用",
            style = MaterialTheme.typography.labelLarge,
            color = MaterialTheme.colorScheme.secondary,
            modifier = Modifier.padding(horizontal = 28.dp))
        NavigationDrawerItem(
            label = { Text("青龙面板") },
            icon = { Icon(Icons.Filled.KeyboardArrowDown, null) },
            selected = currentRoute == NasDestinations.SETTING,
            onClick = {
                navDestination.navigateToQl(context)
                closeDrawer()
            },
            modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding))

    }
}

@Preview
@Composable
private fun DrawerTop(modifier: Modifier = Modifier) {
    Column(modifier = modifier) {
        Text(stringResource(R.string.app_name),
            style = MaterialTheme.typography.titleLarge,
            color = MaterialTheme.colorScheme.primary)
        Spacer(Modifier.height(8.dp))
        Text("天天学习",
            style = MaterialTheme.typography.titleSmall,
            color = MaterialTheme.colorScheme.secondary)
    }
}

@Preview("Drawer contents")
@Preview("Drawer contents (dark)", uiMode = UI_MODE_NIGHT_YES)
@Composable
fun PreviewAppDrawer() {
    NasToolBoxTheme {
//        AppDrawer(currentRoute = "JetnewsDestinations.HOME_ROUTE", navDestination = , closeDrawer = {})
    }
}